package parser;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.text.SimpleDateFormat;
import java.util.Date;
        

public class Parser
{
 final static int DATA=0;
 final static int CENA=1;
 final static int ADRES=2;
 final static int OPIS=3;
 private ArrayList<Stancja> array;
    
 public Parser()
 {
   array=new ArrayList<Stancja>();
 }
    
 public void findMatcher() throws MalformedURLException, IOException 
 {
     URL url = new URL("http://wroclaw.gumtree.pl/f-Nieruchomosci-dom-mieszkanie-wynajme-W0QQCatIdZ9008");
     BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
     
     Pattern main_pat = Pattern.compile("<td class=\" hgk\">.*<a href=\"(.*[0-9$])\".*>(.*)</a>");
     Matcher match;
     String input,link,output=" ";
     while((input = in.readLine())!=null)  
     {
          output=output.concat(input);
     }
     Pattern div = Pattern.compile("</td>");
     String[] divided = div.split(output);
     for(String t: divided)
     {
      match=main_pat.matcher(t);
      if(match.find())
      {
        link=match.group(1);
        Stancja s=saveInfo(link);
        String tytul=match.group(2);
        tytul= swapPolishChar(tytul);
        s.setTytul(tytul);
        addToArray(s);
      }
    }
  

  }
   public Stancja saveInfo(String stancja)throws MalformedURLException, IOException
   {
     URL url = new URL(stancja);
     BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
     String[] info_parts = {"<td.*>Data dodania</td><td.*>(.*)</td></tr>",
                            "<td.*>Cena</td><td.*><strong>Zl(.*),00</strong></td></tr>",
                            "<td.*>Adres</td><td.*>(.*)<br>.*</td></tr>",
                            "<span id=\"preview-local-desc\">(.*)</span><!-- google_ad_section_end -->"};
     Matcher match;
     Pattern pat;
     String input,data,output="";
     String[] tab;
     
     Stancja st=new Stancja();
     
     
     while((input = in.readLine())!=null)  
     {
          output = output.concat(input);
     }
     output = deleteChars("(\n|\t|\r)",output);
     output=swapPolishChar(output);
     tab=splitWith(output,"<tr>");
     for(String t: tab)
     {
      for(int i=0; i<info_parts.length-1; i++)
      { 
         pat=Pattern.compile(info_parts[i]);
         match=pat.matcher(t);
         if(match.find())
         {
           data=match.group(1);
           switch(i)
           {
             case DATA:  data = deleteChars("\\s",data);
                         try{String date=setDate(data);
                                st.setData(date);}
                            catch (Exception e) {e.getMessage();}
                           break;
             case CENA:  data = deleteChars("\\s",data);
                         st.setCena(data);    
                         break;
             case ADRES: data = deleteChars(",",data);
                         st.setAdres(data); break;
             default:    break;
           }
         }
      }
    }
    tab=splitWith(output,"<!-- google_ad_section_start -->"); 
    for(String t: tab)
    {
      pat=Pattern.compile(info_parts[OPIS]);
      match=pat.matcher(t);
      if(match.find()) 
      {
        data=match.group(1);
        data = deleteChars("\\t|\\s|<.*>|&nbsp|'",data);
        st.setOpis(data);
      }
    }
    
    return st;
   }
   
   public static String replace(String beforeReg,String afterReg, String candidate)
   {
    
     Pattern pat = Pattern.compile(beforeReg);
     Matcher matcher = pat.matcher(candidate);
     String temp = matcher.replaceAll(afterReg);
     return temp;
   }
   
   public ArrayList<Stancja> removeUnnecessery(ArrayList<Stancja> list)
   {
     for(int i=0;i<list.size();i++)
     {
       Stancja s=(Stancja)list.get(i);
       if(s.getTytul()=="" ||  s.getAdres()=="" || s.getData()=="" || s.getCena()==""|| s.getOpis()=="")
       removeFromArray(i); 
     }
   return list;
   }
   public String[] splitWith(String input, String regex)
   {
     Pattern pat = Pattern.compile(regex);
     return pat.split(input);
   }
   
   public void addToArray(Stancja stancja)
   {
     array.add(stancja);
   } 
   
   public ArrayList<Stancja> getArray()
   {
    return array;
   }
   
   public String deleteChars(String afterReg, String s)
   {
       s = replace(afterReg,"",s);
       return s;
   }
   
   public void removeFromArray(int index)
   {
     array.remove(index);
   } 
   
   public String setDate(String d) throws Exception
   {
    
    SimpleDateFormat sdfSource = new SimpleDateFormat("dd/MM/yyyy");
    Date date = sdfSource.parse(d);
    SimpleDateFormat sdfDest = new SimpleDateFormat("yyyy-MM-dd");
    String s = sdfDest.format(date);
    return s;
   }
   
   public String swapPolishChar(String data)
   {
    data = replace("ł","l",data);
    data = replace("ą","a",data);
    data = replace("ę","e",data);
    data = replace("ż|ź","z",data);
    data = replace("ć","c",data);
    data = replace("ó","o",data);
    data = replace("ń","n",data);
    data = replace("ś","s",data);
    data = replace("Ł","L",data);
    data = replace("Ą","A",data);
    data = replace("Ę","E",data);
    data = replace("Ż|Ź","Z",data);
    data = replace("Ć","C",data);
    data = replace("Ó","O",data);
    data = replace("Ń","N",data);
    data = replace("Ś","S",data);
    return data;
   }
   public void findAll()
   {
     try{try {findMatcher();} 
      catch (MalformedURLException mue) {System.out.println(mue.getMessage());}}
       catch (IOException ioe) {System.out.println(ioe.getMessage());}
   }
   
}